package com.beidouapp.service.impl;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beidouapp.cache.JbossCacheProvider;
import com.beidouapp.dao.DictDao;
import com.beidouapp.domain.pojo.Dict;
import com.beidouapp.service.DictService;

@Service(value = "dictService")
public class DictServiceImpl implements DictService {
    private static final String DICT_CACHE_FQN_ROOT = "/dict";
    private static final String DICT_CACHE_TYPE_CODE_PRIFIX = "/dcitType_";
    private static final String CACHE_NODE_MAP_KEY = "dictInstance";

    @Autowired
    private DictDao dictDao;
    @Autowired
    private JbossCacheProvider m_cacheProvider;

    public void setDictDao(DictDao dictDao) {
        this.dictDao = dictDao;
    }

    public void setCacheProvider(JbossCacheProvider cacheProvider) {
        m_cacheProvider = cacheProvider;
    }

    @Override
    public List<Dict> getChildDict(String dictTypeCode, String dictCodeFqn) {
        List<Dict> result = new ArrayList<Dict>();
        StringBuffer sbFqn = new StringBuffer();
        sbFqn.append(DICT_CACHE_FQN_ROOT);
        sbFqn.append(DICT_CACHE_TYPE_CODE_PRIFIX);
        sbFqn.append(dictTypeCode);
        if (dictCodeFqn != null) {
            sbFqn.append(dictCodeFqn);
        }
        Map<String, Object> dictMap = m_cacheProvider.findChildNodes(sbFqn.toString());
        if (dictMap != null) {
            for (Object dictO : dictMap.values()) {
                result.add((Dict) dictO);
            }
        }
        return result;
    }

    @Override
    public List<Dict> getChildDictByEnableState(String dictTypeCode, String dictCodeFqn, Boolean isEnable) {
        assert isEnable != null : "param isEnable can't be null";

        List<Dict> result = new ArrayList<Dict>();
        StringBuffer sbFqn = new StringBuffer();
        sbFqn.append(DICT_CACHE_FQN_ROOT);
        sbFqn.append(DICT_CACHE_TYPE_CODE_PRIFIX);
        sbFqn.append(dictTypeCode);
        if (dictCodeFqn != null) {
            sbFqn.append(dictCodeFqn);
        }
        Map<String, Object> dictMap = m_cacheProvider.findChildNodes(sbFqn.toString());
        if (dictMap != null) {
            for (Object dictO : dictMap.values()) {
                Dict dict = (Dict) dictO;
                if (dict != null && dict.getIsEnabled().equals("1")) {
                    result.add((Dict) dictO);
                }

            }
        }

        Collections.sort(result, new Comparator<Dict>() {
            @Override
            public int compare(Dict o1, Dict o2) {
                if (o1 == null || o2 == null) {
                    return -1;
                }
                if (o1.getDictItemIndex() > o2.getDictItemIndex()) {
                    return 1;
                } else if (o1.getDictItemIndex() < o2.getDictItemIndex()) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });

        return result;
    }

    @Override
    public Dict getDict(String dictTypeCode, String dictCodeFqn) {
        StringBuffer sbFqn = new StringBuffer();
        sbFqn.append(DICT_CACHE_FQN_ROOT);
        sbFqn.append(DICT_CACHE_TYPE_CODE_PRIFIX);
        sbFqn.append(dictTypeCode);
        sbFqn.append(dictCodeFqn);
        return (Dict) m_cacheProvider.getCacheValue(sbFqn.toString(), CACHE_NODE_MAP_KEY);
    }

    @Override
    public List<Dict> findAllDict() {
        return dictDao.findAllDict(false);
    }

}
